luasql = require "luasql.mysql"

function printTable(tb, szsplit)
    if type(tb) == "table" then
        print(">>>>>>>>>>>>>>>>>>>>>>>>>", szsplit)
        for name,val in pairs(tb) do
            print(name,val)
        end
        print(">>>>>>>>>>>>>>>>>>>>>>>>>", szsplit)
    else
        return
    end
end

env = assert(luasql.mysql())

conn = assert(env:connect('', 'root', 'chaos@', '39.106.98.31', 3306))

conn:execute"SET NAMES UTF8"

-- status,errorString = conn:execute([[DROP TABLE IF EXISTS user ;]])
-- print(status,errorString)

-- status,errorString = conn:execute([[CREATE TABLE user (id INTEGER, name TEXT);]])
-- print(status,errorString)
--

local dbname = 'test'

status , errorString = conn:execute([[DROP DATABASE IF EXISTS ]]..dbname)

status , errorString = conn:execute([[CREATE DATABASE ]]..dbname)
print(status,errorString)

conn = assert(env:connect(dbname, 'root', 'chaos@', '39.106.98.31', 3306))

-- cur = conn:execute("select * from user")

-- local szcolnames = cur:getcolnames()
-- printTable(szcolnames, "szcolnames")

-- local szcoltypes =cur:getcoltypes()
-- printTable(szcoltypes, "szcoltypes")

-- local dwRows = cur:numrows()
-- print(dwRows, "dwRows")

-- row = cur:fetch({},"localhost")

-- for key,value in pairs(row) do
--     print(key,value)
-- end

function rows (connection, sql_statement)
  local cursor = assert (connection:execute (sql_statement))
  return function ()
    return cursor:fetch()
  end
end

-- for name,email,password, select_priv  in rows(conn ,"SELECT * from user") do
--     print(string.format("%s  %s %s %s",name,email,password ,select_priv))
-- end

conn:close()
env:close()
